home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_400
/
423_01
/
accpost
/
post.c
< prev
next >
Wrap
Text File
|
1990-08-31
|
28KB
|
1,113 lines
/* Plain Vanilla Posting II
Copyright 1990 Plain Vanilla Corporation
P.O. Box 4493, San Diego CA 92164
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#include <stdio.h>
#include <ctype.h>
#include <setjmp.h>
#include <string.h>
#include <dos.h>
#include <alloc.h>
static char id[] = {0x17,0xFD,0x46,0x0F,0x74,0xB3};
static char s1[] = "Changeable journal name (0=no, 1=yes)";
static int changeable_journal_name = 0;
static char sx[] = "";
#define MAXIMUM_NAME_OR_FILESPEC_LENGTH 64
#define MAXIMUM_FILESPEC_LENGTH 64
#define MAXIMUM_NAME_LENGTH 40
#define JOURNAL_NAME_LENGTH 12
typedef long amount_type;
extern amount_type multiply_10(amount_type);
extern amount_type divide_10(amount_type);
extern amount_type sum(amount_type, amount_type);
extern amount_type negative(amount_type);
extern int remainder_10(amount_type);
#define AMOUNT_WIDTH 13
#define isdebit(x) ((x)>0)
#define iscredit(x) ((x)<0)
#define iszero(x) ((x)==0)
#define zero 0L
static char *edit_line_number(unsigned n)
{
static char s[] = {'x','x','x','x',0};
int i;
for (i=3; i>=0; i--, n/=10) s[i] = n%10+'0';
return s;
}
/* case-insensitive strcmp() */
static int ci_strcmp(char *s, char *t)
{
while (*s!=0 && toupper(*s)==toupper(*t)) {s++; t++;}
return toupper(*s)-toupper(*t);
}
static struct
{
jmp_buf jump_buffer;
char *message;
char *name;
int level;
} err;
struct file
{
int c;
FILE *fp;
char *command_line_argument_pointer;
unsigned line;
int indented, new_entry;
char specs[MAXIMUM_FILESPEC_LENGTH+1];
};
static struct file source;
static FILE *output;
static char output_specs[MAXIMUM_FILESPEC_LENGTH+1];
#define error() setjmp(err.jump_buffer)
static void error_message(void)
{
if (source.specs[0]!=0) fputs(source.specs, stdout);
if (source.line!=0)
{
putchar(':');
fputs(edit_line_number(source.line), stdout);
}
putchar(' ');
fputs(err.message,stdout);
if (err.name!=NULL)
{
fputs(" -- ", stdout);
fputs(err.name, stdout);
}
putchar('\n');
err.level = 1;
}
static void error_exit(void)
{
error_message();
exit(err.level);
}
static void raise_error_with_name(char *message, char *name)
{
err.message = message;
err.name = name;
longjmp(err.jump_buffer, 1);
}
void raise_error(char *message)
{
raise_error_with_name(message, NULL);
}
static void declare_error_with_name(char *message, char *name)
{
err.message = message;
err.name = name;
error_message();
}
static void declare_error(char *message)
{
declare_error_with_name(message, NULL);
}
static void open_source_file(char *specs)
{
if (specs[0]=='[')
{
source.command_line_argument_pointer = specs+1;
specs = "[]";
}
else
{
if (specs[0]=='+') specs++;
source.fp = fopen(specs,"r");
if (source.fp==NULL)
raise_error_with_name("Can't open source file", specs);
source.command_line_argument_pointer = NULL;
}
strcpy(source.specs, specs);
source.c = '\n';
source.line = 0;
}
static void read_character(void)
{
if (source.c!=EOF)
{
if (source.c=='\n' && source.line<9999) source.line++;
if (source.command_line_argument_pointer==NULL)
source.c = getc(source.fp);
else
{
source.c = *source.command_line_argument_pointer++;
if (source.c==0) source.c = EOF;
}
}
}
static void close_source_file(void)
{
if (source.command_line_argument_pointer==NULL);
fclose(source.fp);
}
static void invalid_amount(void)
{
raise_error("Invalid dollar amount");
}
static void skip_spaces(void)
{
while (source.c==' ' || source.c=='\t') read_character();
}
static void next_line(void)
{
source.indented = source.new_entry = 0;
while (1)
{
source.indented = 0;
while (source.c!='\n')
{
if (source.c==EOF) {source.new_entry = 1; return;}
read_character();
}
read_character();
if (source.c==' ' || source.c=='\t') source.indented = 1;
skip_spaces();
if (isalpha(source.c)) break;
if (source.c=='\n' || source.c==EOF) source.new_entry = 1;
else if (source.c!='*') declare_error("Questionable journal line");
}
}
static amount_type read_amount(void)
{
int commas = 0;
int cents = 0;
int position = 0;
int nothing = 1;
amount_type amount;
amount = zero;
skip_spaces();
while (1)
{
if (source.c==',')
{
if (nothing || cents || commas && position!=4 || !commas && position>3)
invalid_amount();
commas = 1;
position = 0;
commas = 1;
}
else if (source.c=='.')
{
if (nothing || cents || commas && position!=4) invalid_amount();
cents = 1;
commas = 0;
position = 0;
}
else if (isdigit(source.c))
{
nothing = 0;
if (commas && position==4 || cents && position==3) invalid_amount();
amount = sum(multiply_10(amount), (amount_type)(source.c-'0'));
}
else break;
read_character();
position++;
}
if (nothing || cents && position!=3 || commas && position!=4)
invalid_amount();
if (!cents)
{
amount = multiply_10(amount);
amount = multiply_10(amount);
}
return amount;
}
static unsigned read_number(void)
{
int no_number = 1;
unsigned number = 0;
skip_spaces();
while (isdigit(source.c))
{
if (number>6552) {number = 0xFFFF; break;}
number = 10*number + source.c - '0';
no_number = 0;
read_character();
}
if (no_number) number = 0xFFFF;
return number;
}
static int invalid_file_character(int c)
{
return c<=' ' || c=='"' || c=='/' || c=='[' || c==']' ||
c=='|' || c=='<' || c=='>' || c=='+' || c=='=' ||
c==';' || c==',';
}
static void read_specs(char *specs)
{
int i = 0;
skip_spaces();
if (source.c=='+')
{
specs[i++] = '+';
read_character();
}
if (invalid_file_character(source.c))
raise_error("Invalid or missing file specifications");
while (!invalid_file_character(source.c))
{
if (i<MAXIMUM_FILESPEC_LENGTH) specs[i++] = source.c;
read_character();
}
specs[i] = 0;
skip_spaces();
}
static void read_name(char *name)
{
int i = 0;
skip_spaces();
if (!isalpha(source.c))
raise_error("Invalid or missing name");
do
{
do
{
if (i<MAXIMUM_NAME_LENGTH) name[i++] = source.c;
read_character();
} while (isalnum(source.c) || source.c=='/');
if (i<MAXIMUM_NAME_LENGTH) name[i++] = ' ';
skip_spaces();
} while (isalpha(source.c));
if (name[i-1]==' ') i--;
name[i] = 0;
}
static void *get_memory(int n)
{
void *g = malloc((unsigned long)n);
char *t;
if (g==NULL)
{
puts("\nInsufficient memory");
exit(1);
}
for (t=g; n>0; n--) *t++ = 0;
return g;
}
static void open_output_file(char *specs)
{
if (specs==NULL) output = stdout;
else
{
if (specs[0]=='+') output = fopen(specs+1,"a");
else output = fopen(specs,"w");
if (output==NULL) raise_error_with_name("Can't open output file", specs);
strcpy(output_specs, specs);
}
}
static void close_output_file(void)
{
if (output!=stdout && output!=NULL) fclose(output);
output = NULL;
}
static void write_character(int c)
{
if (output==stdout) putchar(c);
else if (putc(c,output)==EOF)
{
close_output_file();
raise_error_with_name("File write error", output_specs);
}
}
static void write_string(char *s)
{
while (*s!=0) write_character(*s++);
}
static void write_amount(amount_type amount, int trim)
{
char s[AMOUNT_WIDTH+1];
register char *t = s+sizeof(s);
*--t = 0;
*--t = remainder_10(amount)+'0'; amount = divide_10(amount);
*--t = remainder_10(amount)+'0'; amount = divide_10(amount);
*--t = '.';
do
{
if (t==s+7 || t==s+3) *--t = ',';
*--t = remainder_10(amount)+'0';
amount = divide_10(amount);
} while (!iszero